package com.example.myapplication11.ui.gallery;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.TimePicker;

import androidx.annotation.Nullable;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.myapplication11.R;

import java.util.Calendar;

public class GalleryFragment extends Fragment {
    TextView showTimeText = null;
    int hour = 0;
    int minute = 0;
    private GalleryViewModel galleryViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                ViewModelProviders.of(this).get(GalleryViewModel.class);
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        final TextView textView = root.findViewById(R.id.text_gallery);
        galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        initView(root);
        showTime(hour,minute);


        return root;
    }

    private void initView(View root){
        Calendar cal = Calendar.getInstance();
        hour = cal.get(Calendar.HOUR_OF_DAY);
        minute = cal.get(Calendar.MINUTE);
        showTimeText = (TextView) root.findViewById(R.id.fragment_gallery_showTimeText);
        // 时间显示控件
        TimePicker timePicker = (TimePicker) root.findViewById(R.id.fragment_gallery_showTime);
        // 设置监听
        timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
                showTime(hour,minute);
            }
        });

    }

    private void showTime(int hour2,int minute2){
        showTimeText.setText("时间" + hour2 +":" + minute2);
    }

}